package ga.wavaplayer.net {
	import ga.wavaplayer.events.WNetEvent;
	import flash.events.NetStatusEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	
	public class WNetConnection extends NetConnection{
		
		public var current_bitrate:Number = 0;
		public var current_latency:Number = 0;
		
		public function WNetConnection() {
			this.addEventListener(NetStatusEvent.NET_STATUS, _netStatusHandler);
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _securityErrorHandler);
			this.addEventListener(AsyncErrorEvent.ASYNC_ERROR, _asyncErrorHandler);
		}
		
		public function onBWDone(pBitrate:Number = 0, pUnused1:Number = 0, pUnused2:Number = 0, pLatency:Number = 0):void {
			current_bitrate = pBitrate;
			current_latency = pLatency;
			dispatchEvent(new WNetEvent(WNetEvent.BANDWIDTH_CHECK_DONE));
		}
		
		public function onBWCheck(... rest):Boolean {
			dispatchEvent(new WNetEvent(WNetEvent.BANDWIDTH_CHECK));
			
			return true;
		}
		
		private function _netStatusHandler(e:NetStatusEvent):void {
			if (e.info.code == "NetConnection.Connect.Success") {
				dispatchEvent(new WNetEvent(WNetEvent.CONNECTED));
			} else {
				dispatchEvent(new WNetEvent(WNetEvent.CONNECTION_FAILED));
			}
		}
		
		private function _securityErrorHandler(e:SecurityErrorEvent):void {}
        private function _asyncErrorHandler(e:AsyncErrorEvent):void {}
	}
}
